package day01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 列表操作： 增删改查
 */
public class ListTest {

    public List coursesToSelect;

    public ListTest() {
        this.coursesToSelect = new ArrayList();
    }

    public void testAdd () {
        // 从头部插入
        Course c1 = new Course("1", "java");
        coursesToSelect.add(c1);
        // 指定下标获取
        Course temp = (Course) coursesToSelect.get(0);
        System.out.println("新增课程 ： " + temp.id + " : " + temp.name);

        // 指定下表 插入
        Course c2 = new Course("2", "php");
        coursesToSelect.add(0, c2);
        Course temp2 = (Course) coursesToSelect.get(0);
        System.out.println("新增课程 ： " + temp2.id + " : " + temp2.name);

        // 同时插入多个
        Course[] courses = {new Course("3", "vue"), new Course("4", "nodejs")};

        coursesToSelect.addAll(Arrays.asList(courses));
        Course temp3 = (Course) coursesToSelect.get(2);
        Course temp4 = (Course) coursesToSelect.get(3);
        System.out.println("新增课程 ： " + temp3.id + " : " + temp3.name);
        System.out.println("新增课程 ： " + temp4.id + " : " + temp4.name);

        Course[] courses1 = {
                new Course("5", "lua"),
                new Course("6", "golang"
        )};
        coursesToSelect.addAll(2, Arrays.asList(courses1));
        Course temp5 = (Course) coursesToSelect.get(2);
        Course temp6 = (Course) coursesToSelect.get(3);
        System.out.println("新增课程 ： " + temp5.id + " : " + temp5.name);
        System.out.println("新增课程 ： " + temp6.id + " : " + temp6.name);
    }

    public void testGet() {
        int size = coursesToSelect.size();
        System.out.println("使用for打印数据");
        for (int i = 0; i < size; i++) {
            Course cr = (Course) coursesToSelect.get(i);
            System.out.println("子对象：" + cr.id + " : " + cr.name);
        }
    }

    public void testIterator() {
        Iterator<Course> it = coursesToSelect.iterator();
        System.out.println("使用Iterator遍历List");
        while (it.hasNext()) {
            Course cr = it.next();
            System.out.println("子对象：" + cr.id + " : " + cr.name);
        }
    }

    public void testForEach() {
        System.out.println("使用For each遍历LIst: ");
        for(Object obj : coursesToSelect) {
            Course cr = (Course) obj;
            System.out.println("子对象：" + cr.id + " : " + cr.name);
        }
    }

    /**
     * 修改
     */
    public void testModify() {
        coursesToSelect.set(4, new Course("7", "go"));
    }

    public void testRemove() {
        System.out.println("移除数据");
        Course[] courses = {
            (Course) coursesToSelect.get(4),
            (Course) coursesToSelect.get(5)
        };
//        coursesToSelect.remove(4);
        coursesToSelect.removeAll(Arrays.asList(courses));
        System.out.println("删除完成");
        testForEach();
    }

    public void testType() {
        System.out.println("添加不同类型的数据");
        coursesToSelect.add("我是String数据");
    }

    public static void main(String[] args) {
        ListTest lt = new ListTest();
        lt.testAdd();

//        lt.testType();
        lt.testForEach();


//        lt.testRemove();
//        lt.testGet();
        lt.testIterator();
//        lt.testModify();
    }
}
